<template>
    <dzs-dialog :title="title" :isShow="isShow" @close="onCancel" @open="onOpen">
        <dzs-form height="500px" :loadingOption="formInfo" :status="status" ref="myForm" v-model:value="formData"
            :options="formOptions" @onCancel="onCancel" @onSubmit="onSubmit">
        </dzs-form>
    </dzs-dialog>
</template>
<script setup lang="ts">
import { ref, computed } from "vue";
import type { dzsFormProps } from "@/components/dzs-form/@types";
import type { dzsFormInstance } from "@/components/dzs-form";
import type { formData } from "../@types";
import { ElNotification } from 'element-plus';
import { addMyProject, updateMyProject } from "../api/api";
import type { tableItem } from "../@types";
const emit = defineEmits(['cancel', 'update:isShow', 'onHoldSearch'])
const props = defineProps<{
    dialogData?: tableItem,
    status: string,
    isShow: boolean
}>()

const myForm = ref<dzsFormInstance>();
const formData = ref<formData>({
    ID: "",
    title : '',
    time : '',
    introduce : '',
    duty : '',
    technology : '',
    sorts: 99,
    is_show: 1,
})

const formInfo = ref({
    loading: false,
})

const title = computed(() => {
    return props.status === "add" ? "添加我的项目" : props.status === "edit" ? "编辑我的项目" : "查看我的项目详情"
})
const formOptions = computed<dzsFormProps['formOptions']>(() => {
    return {
        formProps: {
            "labelWidth": "100px"
        },
        formItem: [
            {
                label: "ID",
                type: "input",
                key: "ID",
                isHidden: true,
            },
            {
                label: "项目名称",
                type: "input",
                key: "title",
                rules: [
                    { required: true, message: '请输入项目名称', trigger: 'blur' },
                ]
            },
            {
                label: "项目周期",
                type: "input",
                key: "time",
                rules: [
                    { required: true, message: '请输入项目周期', trigger: 'blur' },
                ]
            },
            {
                label: "项目简介",
                type: "input",
                key: "introduce",
                rules: [
                    { required: true, message: '请输入项目简介', trigger: 'blur' },
                ],
                props : {
                    type : 'textarea',
                    rows : 6
                }
            },
            {
                label: "负责内容",
                type: "input",
                key: "duty",
                rules: [
                    { required: true, message: '请输入负责内容', trigger: 'blur' },
                ],
                props : {
                    type : 'textarea',
                    rows : 4
                }
            },
            {
                label: "主要技术",
                type: "input",
                key: "technology",
                rules: [
                    { required: true, message: '请输入主要技术', trigger: 'blur' },
                ],
                props : {
                    type : 'textarea',
                    rows : 4
                }
            },
            {
                label: "排序",
                type: "number",
                key: "sorts",
                rules: [
                    { required: true, message: '请输入排序', trigger: 'blur' },
                ],
                props : {
                    min : 1,
                    max : 999
                }
            },
            {
                label: "是否展示",
                type: "switch",
                key: "is_show",
                props: {
                    "active-value": 1,
                    "inactive-value": 0
                }
            },
        ]
    }
})

const onCancel = () => {
    myForm.value?.onCancel<formData>(false, {
        success: (res) => {
            formData.value = res
        }
    })
    emit('cancel');
    emit('update:isShow', false);
}


const onOpen = () => {
    if (props.dialogData?.ID) {
        formData.value = props.dialogData
    }
}

const onSubmit = () => {
    formInfo.value.loading = true;
    if (props.status === "edit") {
        updateMyProject({
            ...formData.value
        }).then(res => {
            formInfo.value.loading = false;
            ElNotification({
                title: '成功',
                message: "提交成功",
                type: 'success'
            })
            emit("onHoldSearch");
            onCancel();
        }).catch(() => {
            formInfo.value.loading = false;
        })
        return;
    }
    addMyProject({
        ...formData.value
    }).then(res => {
        formInfo.value.loading = false;
        ElNotification({
            title: '成功',
            message: "提交成功",
            type: 'success'
        })
        emit("onHoldSearch");
        onCancel();
    }).catch(() => {
        formInfo.value.loading = false;
    })
}
</script>
<style scoped></style>